package com.android.lovingpet.signup.view;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.signup.ILoginContract;
import com.android.lovingpet.signup.LoginInPresenter;
import com.android.lovingpet.util.TimeCount;
import com.android.lovingpet.util.Utility;


/**
 * Created by ikata on 2018/1/17.
 */
public class LoginInYzmFragment extends BaseFragment<ILoginContract.View,LoginInPresenter> implements View.OnClickListener, ILoginContract.View  {
    private TimeCount time;
    private TextView tv4;
    private EditText et_verification_code;

    @Override
    protected int setContentView() {
        return R.layout.fragment_login_in_yzm;
    }

    @Override
    protected void onCreateView(View view) {
        ((TextView) view.findViewById(R.id.tv_title)).setText("验证码");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        tv4=getActivity().findViewById(R.id.login_in_text4);
        tv4.setText(getPresenter().getPhone());
        TextView tv2=getActivity().findViewById(R.id.login_in_text2);
        Button bt1=getActivity().findViewById(R.id.login_in_ok);
        Button bt2=getActivity().findViewById(R.id.login_in_get);
        et_verification_code=getActivity().findViewById(R.id.login_in_security_code);
        tv2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv2.getPaint().setAntiAlias(true);
        bt2.setOnClickListener(this);
        //确定验证码
        bt1.setOnClickListener(this);
        //倒计时开启
        time=new TimeCount(getContext(),bt2,60000,1000);
        time.start();
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        reBind();
    }

    @Override
    public void onDestroyView() {
        time.onFinish();
        super.onDestroyView();
    }

    @Override
    public void next() {
        if (Utility.getUserType() == 1) {
            getFragmentManager()
                    .beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.login_in_count, new LoginInMasterDataFragment())
                    .commit();
        } else {
            getFragmentManager()
                    .beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.login_in_count, new LoginInTeamFragment())
                    .commit();
        }
    }
    @Override
    public LoginInPresenter createPresenter() {
        //return new LoginInPresenter();
        return ((LoginInActivity) getActivity()).getPresenter();
    }

    @Override
    public ILoginContract.View createView() {
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_in_text2:
                //用户协议
                break;

            case R.id.login_in_get:
                time.start();
                getPresenter().sendCode();
                break;

            case R.id.login_in_ok:
                getPresenter().auth(et_verification_code.getText().toString());
                break;

            default:
                break;

        }
    }
}
